package com.ruoyi.api.activiti.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.activiti.service.IProcessService;
import com.ruoyi.api.activiti.domain.ApplyAssetsReport;
import com.ruoyi.api.mqtt.MqttService;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.SysUser;
import com.ruoyi.system.mapper.SysUserMapper;
import lombok.extern.slf4j.Slf4j;
import org.activiti.engine.HistoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.runtime.ProcessInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.HashMap;

@Service
@Slf4j
public class ApplyAssetsReportService extends UsualWrokService{
  @Value("${wspaas.dailyworkUrl}")
  private String dwUrl;
  @Autowired
  private RuntimeService runtimeService;
  @Autowired
  private TaskService taskService;
  @Autowired
  private HistoryService historyService;
  @Autowired
  private SysUserMapper userMapper;
  @Autowired
  private IProcessService processService;
  @Autowired
  private UsualWrokService usualWrokService;

  @Autowired
  private MqttService mqttService;

  public ProcessInstance submitApplyMobile(ApplyAssetsReport applyAssetsReport, String loginName, String key, HashMap<String, Object> variables, String service) {
    SysUser sysUser = userMapper.selectUserByLoginName(loginName);
    if (sysUser == null) {
      throw new RuntimeException("用户不存在");
    }
    JSONObject jsonObject = findById(applyAssetsReport.getId(), service);
    ApplyAssetsReport tmp = JSON.parseObject(jsonObject.toJSONString(),ApplyAssetsReport.class);
    if(tmp == null){
      throw new RuntimeException("业务数据不存在");
    }
    applyAssetsReport.setApplyUserName(sysUser.getUserName());
    applyAssetsReport.setCreaterName(loginName);
    applyAssetsReport.setApplyTime(DateUtils.getNowDate().getTime());
    String businessKey = applyAssetsReport.getId().toString(); // 实体类 ID，作为流程的业务 key
    variables.put("applyUserId", loginName);
    variables.put("businessKey", businessKey);
    variables.put("frontAssign", tmp.getAuditLoginName());
    if(tmp.getProjectId() !=null)
      variables.put("projectId", tmp.getProjectId());

    int maxRole = getProjectMaxRole(sysUser);
    if(maxRole== 3){
      variables.put("creater",3);
    }else if(maxRole == 2){
      variables.put("creater",2);
    }else if(maxRole== 1){
      variables.put("creater",1);
    }else {
      throw new RuntimeException("申请人角色非法");
    }
    update(JSON.toJSONString(applyAssetsReport),1,service);
    ProcessInstance processInstance = processService.submitApply(loginName, businessKey, "申请", applyAssetsReport.getReason(), key, variables);
    return processInstance;
  }

  /**
   * 撤销
   */
  public void cancelApply(String entityId) {
    ApplyAssetsReport applyAssetsReport = new ApplyAssetsReport();
    applyAssetsReport.setId(entityId);
    applyAssetsReport.setSuspendState("3");
    applyAssetsReport.setTaskName("已撤销");
    update(JSON.toJSONString(applyAssetsReport), 1,"applyassets");
  }



}
